package com.itheima.crm.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.common.utils.Page;

import com.itheima.crm.pojo.Application;
import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.RiZhi;
import com.itheima.crm.pojo.YuanGong;
import com.itheima.crm.service.ApplicationService;
import com.itheima.crm.service.BaseDictService;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class ApplicantController {

	@Autowired
	private ApplicationService applicationService;
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private RiZhiService rizhiService;
	

	public ApplicationService getApplicationService() {
		return applicationService;
	}






	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}






	public YuanGongService getYuangongService() {
		return yuangongService;
	}






	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}






	public BaseDictService getBaseDictService() {
		return baseDictService;
	}






	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}






	public RiZhiService getRizhiService() {
		return rizhiService;
	}






	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}






	// Application首页查看
	@RequestMapping(value = "/Application")
	public String Applicationlist(Application vo, Model model,HttpServletResponse response,
			HttpServletRequest request) {
		Integer applicantid=applicationService.MaxApplicationId();
	applicantid=applicantid+1;
		System.out.println(applicantid);
		
		request.getSession().setAttribute("app_numberinsert", applicantid);
		List<BaseDict> typeType = baseDictService.selectBaseDictListByCode("004");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> statusType = baseDictService.selectBaseDictListByCode("005");
		List<BaseDict> resultType = baseDictService.selectBaseDictListByCode("006");
		model.addAttribute("typeType", typeType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("statusType", statusType);
		model.addAttribute("resultType", resultType);
		      HttpSession session=request.getSession(false);
		String loginname = (String) session.getAttribute("loginname");
		String loginpw = (String) session.getAttribute("loginpw");
		Integer applicant_id = applicationService.selectYGidBylogin(loginname, loginpw);
		YuanGong loginer = yuangongService.selectYuanGongByYGid(applicant_id);
		Integer ygid = loginer.getYgid();
		String zhiwei = loginer.getZhiwei();
		String organization=loginer.getOrganization();
		if(zhiwei.equals("2")) {
		vo.setApplicant_id(ygid);
		vo.setApprove_department(organization);
		Page <Application> page=applicationService.selectMyAndOrganization(vo);
	
			/*vo.setApplicant_id(ygid);
		Page<Application> page = applicationService.selectMyApplication(vo);*/
		model.addAttribute("page", page);
		return "Application";}
		
		else if(zhiwei.equals("1")) {vo.setApplicant_id(ygid);
		Page<Application> page = applicationService.selectMyApplication(vo);
		model.addAttribute("page", page);
		return "Application";}
		return "Application";
		}
	

		  
		  
	

	// Approve首页查看
	@RequestMapping(value = "/Approve")
	public ModelAndView Approvelist(Application vo, Model model, HttpServletResponse response,
			HttpServletRequest request) {
		
		HttpSession session1=request.getSession(false);
		String zhiwei1 = (String) session1.getAttribute("zhiwei");
		
		if (!zhiwei1.equals("1")) {
		
		List<BaseDict> typeType = baseDictService.selectBaseDictListByCode("004");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> statusType = baseDictService.selectBaseDictListByCode("005");
		List<BaseDict> resultType = baseDictService.selectBaseDictListByCode("006");
		model.addAttribute("typeType", typeType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("statusType", statusType);
		model.addAttribute("resultType", resultType);
		HttpSession session=request.getSession(false);
		String loginname = (String) session.getAttribute("loginname");
		String loginpw = (String) session.getAttribute("loginpw");
		Integer applicant_id = applicationService.selectYGidBylogin(loginname, loginpw);
		YuanGong loginer = yuangongService.selectYuanGongByYGid(applicant_id);
		String zhiwei = loginer.getZhiwei();
		String organization = loginer.getOrganization();
		vo.setApprove_department(organization);
		if (!vo.getApprove_department().equals(null) && !zhiwei.equals("1")) {
			ModelAndView a = new ModelAndView();
			Page<Application> page = applicationService.selectOrganizationApplication(vo);
			a.setViewName("Approve");
			a.addObject("page", page);
			return a;
		}

		ModelAndView a = new ModelAndView();
		a.setViewName("Approve");
		return a;}
		else {
			
			ModelAndView a=new ModelAndView();
			
			a.setViewName("error");

		     return a;
		}
	}

	@RequestMapping(value = "/ApplicationFind")
	public String ApplicationFindlist(Application vo, Model model) {
		List<BaseDict> typeType = baseDictService.selectBaseDictListByCode("004");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> statusType = baseDictService.selectBaseDictListByCode("005");
		List<BaseDict> resultType = baseDictService.selectBaseDictListByCode("006");
		model.addAttribute("typeType", typeType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("statusType", statusType);
		model.addAttribute("resultType", resultType);

		Page<Application> page = applicationService.selectApplicationList(vo);
		model.addAttribute("page", page);

		return "ApplicationFind";
	}

	// 筛选我的业务
	@RequestMapping(value = "/myApplication")
	public ModelAndView MyApplicationFind(Application vo, Model model, HttpServletResponse response,
			HttpServletRequest request) {
		// 获取登录时的loginnmae和pw
		List<BaseDict> typeType = baseDictService.selectBaseDictListByCode("004");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> statusType = baseDictService.selectBaseDictListByCode("005");
		List<BaseDict> resultType = baseDictService.selectBaseDictListByCode("006");
		model.addAttribute("typeType", typeType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("statusType", statusType);
		model.addAttribute("resultType", resultType);
		HttpSession session=request.getSession(false);
		String loginname = (String) session.getAttribute("loginname");
		System.out.println(loginname);
		String loginpw = (String) session.getAttribute("loginpw");
		Integer applicant_id = applicationService.selectYGidBylogin(loginname, loginpw);
		System.out.println(applicant_id);
		vo.setApplicant_id(applicant_id);
		Page<Application> page = applicationService.selectMyApplication(vo);
		System.out.println(applicant_id);
		ModelAndView a = new ModelAndView();
		a.setViewName("ApplicationFind");
		a.addObject("page", page);
		a.addObject("id", applicant_id);
		return a;
	}

	// 查看部门业务
	@RequestMapping(value = "/OrganizationApplication")
	public ModelAndView OrgApplicationFind(Application vo, Model model, HttpServletResponse response,
			HttpServletRequest request) {
		// 获取登录时的loginnmae和pw
		List<BaseDict> typeType = baseDictService.selectBaseDictListByCode("004");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> statusType = baseDictService.selectBaseDictListByCode("005");
		List<BaseDict> resultType = baseDictService.selectBaseDictListByCode("006");
		model.addAttribute("typeType", typeType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("statusType", statusType);
		model.addAttribute("resultType", resultType);
		HttpSession session=request.getSession(false);
		String loginname = (String) session.getAttribute("loginname");
		String loginpw = (String) session.getAttribute("loginpw");

		Integer applicant_id = applicationService.selectYGidBylogin(loginname, loginpw);

		System.out.println(applicant_id);

		YuanGong loginer = yuangongService.selectYuanGongByYGid(applicant_id);
		String organization = loginer.getOrganization();
		String zhiwei = loginer.getZhiwei();
		System.out.println(organization + "----" + zhiwei);
		if (!zhiwei.equals("1")) {
			System.out.println("进来了");
			vo.setApprove_department(organization);
			Page<Application> page = applicationService.selectOrganizationApplication(vo);
			ModelAndView a = new ModelAndView();
			a.setViewName("ApplicationFind");
			a.addObject("page", page);
			return a;
		}
		ModelAndView a = new ModelAndView();
		a.setViewName("ApplicationFind");

		return a;

	}
	

	@RequestMapping(value = "/Application/edit.action")
	public @ResponseBody Application editApplication(Integer id) {
		return applicationService.selectApplicationById(id);
	}

	@RequestMapping(value = "/Application/insert.action")
	public @ResponseBody String insertApplication(Application application,HttpServletRequest request) {
		
		

		applicationService.insertApplication(application);
		  RiZhi c=new RiZhi();
			request.getSession().getAttribute("ygid");
			c.setYgid((Integer)request.getSession().getAttribute("ygid"));
			c.setYgname((String)request.getSession().getAttribute("ygname"));
			c.setInsertdate(new Date());
			System.out.println(c.getInsertdate());
			c.setDetial("新建业务申请");
			rizhiService.insertApplication(c);
		return "OK";
	}
	@RequestMapping(value = "/Application/delete.action")
	public @ResponseBody JSONObject deleteApplication(Integer id,HttpServletRequest request) {
		  	RiZhi c=new RiZhi();
			request.getSession().getAttribute("ygid");
			c.setYgid((Integer)request.getSession().getAttribute("ygid"));
			c.setYgname((String)request.getSession().getAttribute("ygname"));
			c.setInsertdate(new Date());
			System.out.println(c.getInsertdate());
			c.setDetial("删除申请");
			rizhiService.insertApplication(c);
		Application application=applicationService.selectApplicationById(id);
	       Date date=application.getApprove_time();
	       System.out.println(date);
	       Date datenow=new Date();
	       System.out.println(datenow);
	       Calendar aCalendar = Calendar.getInstance();
	        aCalendar.setTime(date);
	        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
	        aCalendar.setTime(datenow);
	        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
	        int days=day2-day1;
	        System.out.print(days);
	        JSONObject obj=new JSONObject();
	       
	        if(days>90) {
	        	applicationService.deleteApplication(id);
	        	obj.put("statu", "yes");
	        	
	        }else {
	        	obj.put("statu", "no");
	        }
		return obj;
		
	}
	
	
	@RequestMapping(value = "/Application/nodelete.action")
	public @ResponseBody String nodelete(String id,String reason,HttpServletRequest request) {
		System.out.println(reason);
		System.out.println(id);
		 Integer id1=Integer.parseInt(id);
		applicationService.deleteApplication(id1);
		  RiZhi c=new RiZhi();
			request.getSession().getAttribute("ygid");
			c.setYgid((Integer)request.getSession().getAttribute("ygid"));
			c.setYgname((String)request.getSession().getAttribute("ygname"));
			c.setInsertdate(new Date());
			System.out.println(c.getInsertdate());
			c.setDetial("撤销申请");
			rizhiService.insertApplication(c);
		return JSON.toJSONString("OK");
		
		
	}
	
	

	@RequestMapping(value = "/Approve/update.action")
	public @ResponseBody String updateApprove(Application application,HttpServletRequest request) {
		Date date=application.getApprove_time();
		System.out.println(date);
		System.out.println(application.toString());
		applicationService.updateApproveById(application);
		  RiZhi c=new RiZhi();
			request.getSession().getAttribute("ygid");
			c.setYgid((Integer)request.getSession().getAttribute("ygid"));
			c.setYgname((String)request.getSession().getAttribute("ygname"));
			c.setInsertdate(new Date());
			System.out.println(c.getInsertdate());
			c.setDetial("审批申请");
			rizhiService.insertApplication(c);
		return JSON.toJSONString("ok");
	}

	@RequestMapping(value = "/Application/update.action")
	public @ResponseBody String updateApplication(Application application,HttpServletRequest request) {
		
		
	
		System.out.println(application.getId());
		  RiZhi c=new RiZhi();
			request.getSession().getAttribute("ygid");
			c.setYgid((Integer)request.getSession().getAttribute("ygid"));
			c.setYgname((String)request.getSession().getAttribute("ygname"));
			c.setInsertdate(new Date());
			System.out.println(c.getInsertdate());
			c.setDetial("修改申请");
			rizhiService.insertApplication(c);
		applicationService.updateApplicationById(application);
		
		return "OK";
	}
	// 删除业务
	@RequestMapping(value = "/Approve/delete.action")
	public @ResponseBody String deleteApprove(Integer id,HttpServletRequest request) {
		
		       Application application=applicationService.selectApplicationById(id);
		       Date date=application.getApprove_time();
		       System.out.println(date);
		       Date datenow=new Date();
		       System.out.println(datenow);
		       Calendar aCalendar = Calendar.getInstance();
		        aCalendar.setTime(date);
		        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
		        aCalendar.setTime(datenow);
		        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
		        int days=day2-day1;
		        System.out.print(days);
		        if(days>90) {
		        	applicationService.deleteApplication(id);
		        	  RiZhi c=new RiZhi();
		  			
		  			c.setYgid((Integer)request.getSession().getAttribute("ygid"));
		  			c.setYgname((String)request.getSession().getAttribute("ygname"));
		  			c.setInsertdate(new Date());
		  			System.out.println(c.getInsertdate());
		  			c.setDetial("删除申请");
		  			rizhiService.insertApplication(c);
		        	return JSON.toJSONString("删除成功");
		        }else {
		        	
		        	return JSON.toJSONString("超过三个月才能删除该业务");
		        }
		
		
	}

	// 查询经理
	@RequestMapping(value = "/Application/selectManager.action", method = RequestMethod.POST)
	public @ResponseBody String selectManager(HttpServletRequest request) throws UnsupportedEncodingException {

		String ora = request.getParameter("approve_department");

		String manager = yuangongService.selectManagerByYuanGong(ora);

		String json = JSON.toJSONString(manager);

		return json;
	}

	// 根据id查姓名表
	@RequestMapping(value = "/Application/selectYuanGong.action", method = RequestMethod.POST)
	public @ResponseBody String selectYuanGongById(HttpServletRequest request) throws UnsupportedEncodingException {

		String id = request.getParameter("applicant_id");
		System.out.println(id);
		Integer i = Integer.parseInt(id);

		String name = yuangongService.selectYuanGongByNumber(i);

		String json = JSON.toJSONString(name);

		return json;
	}
	
	

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

}
